package com.handu.luna.core.domain.goods;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Transient;

import com.handu.luna.core.domain.BasePersistentDomain;

@Entity
public class BrandGeneralInfo extends BasePersistentDomain<BrandGeneralInfoRepository>{
	
	//品牌ID
	private long brandId;			
	
	@Transient
	private Brand brand;
	
	//品牌通用信息
	@Lob
	private String content;
	
	//开始时间
	private Date startDate;
	
	//结束时间
	private Date endDate;
	
	//是否显示
	private boolean displayed;
	
	/**
	 * 初始化品牌信息
	 */
	public void init(){
		brand = context.getBrandRepository().findOne(brandId);
	}
	
	/**
	 * 获取品牌通用信息列表（含品牌信息）
	 * @return
	 */
	public List<BrandGeneralInfo> findAll(){
		List<BrandGeneralInfo> brandGeneralInfos = context.getBrandGeneralInfoRepository().findDisplayList();
		for (BrandGeneralInfo brandGeneralInfo : brandGeneralInfos) {
			brandGeneralInfo.init();
		}
		return brandGeneralInfos;
	}
	
	public BrandGeneralInfo() {
	}
	
	public List<BrandGeneralInfo> findAll(String brandName,String isUnexpired){
		
		BrandGeneralInfoRepository brandGeneralInfoRepository = context.getBrandGeneralInfoRepository();
		
		List<BrandGeneralInfo> brandGeneralInfoList = new ArrayList<BrandGeneralInfo>();
		
		if(brandName == null || brandName.equals("")) {
			if(isUnexpired == null) {
				brandGeneralInfoList = brandGeneralInfoRepository.findByUnexpired(new Date());
			}else {
				brandGeneralInfoList = brandGeneralInfoRepository.findDisplayList();
			}
		}else {
			
			List<Brand> brandList = context.getBrandRepository().findByNameLike("%"+brandName+"%");
			
			for(Brand brand : brandList) {
				
				if(isUnexpired == null) {
					brandGeneralInfoList = brandGeneralInfoRepository.findByBrandIdAndUnexpired(brand.getId(),new Date());
				}else {
					brandGeneralInfoList = brandGeneralInfoRepository.findByBrandId(brand.getId());
				}
			}
			
			
		}
		
		for (BrandGeneralInfo brandGeneralInfo : brandGeneralInfoList) {
			brandGeneralInfo.init();
		}
		return brandGeneralInfoList;
	}

	
	
	public BrandGeneralInfo(long brandId, String content,
			Date startDate, Date endDate, boolean displayed) {
		this.brandId = brandId;
		this.content = content;
		this.startDate = startDate;
		this.endDate = endDate;
		this.displayed = displayed;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public long getBrandId() {
		return brandId;
	}

	public void setBrandId(long brandId) {
		this.brandId = brandId;
	}

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public boolean isDisplayed() {
		return displayed;
	}
	public void setDisplayed(boolean displayed) {
		this.displayed = displayed;
	}
	
}
